using System.ComponentModel.DataAnnotations;

namespace AdminSG3L.Domain.Entities;

/// <summary>
/// 文章分类实体
/// </summary>
public class AppArticleCategory : EntityBase
{
    /// <summary>
    /// 分类名称，必填
    /// </summary>
    public string Name { get; private set; }
    
    /// <summary>
    /// 分类编码，必填
    /// </summary>
    public string Code { get; private set; }
    
    /// <summary>
    /// 父分类ID，可选
    /// </summary>
    public Guid? ParentId { get; private set; }
    
    /// <summary>
    /// 父分类
    /// </summary>
    public AppArticleCategory? Parent { get; private set; }
    
    /// <summary>
    /// 子分类集合
    /// </summary>
    public List<AppArticleCategory> Children { get; private set; } = new();
    
    /// <summary>
    /// 该分类下的文章集合
    /// </summary>
    public List<AppArticle> Articles { get; private set; } = new();
    
    /// <summary>
    /// 排序号
    /// </summary>
    public int SortOrder { get; private set; }

    /// <summary>
    /// 私有构造函数，仅限工厂方法调用
    /// </summary>
    private AppArticleCategory(string name, string code)
    {
        Name = name;
        Code = code;
    }

    /// <summary>
    /// 创建AppArticleCategory对象的工厂方法
    /// </summary>
    /// <param name="name">分类名称</param>
    /// <param name="code">分类编码</param>
    /// <param name="parentId">父分类ID</param>
    /// <param name="sortOrder">排序号</param>
    /// <param name="description">描述</param>
    /// <returns>AppArticleCategory对象</returns>
    public static AppArticleCategory Create(string name, string code, Guid? parentId = null, int sortOrder = 0, string? description = null)
    {
        return new AppArticleCategory(name, code)
        {
            ParentId = parentId,
            SortOrder = sortOrder,
            Description = description
        };
    }

    /// <summary>
    /// 更新分类信息
    /// </summary>
    /// <param name="name">分类名称</param>
    /// <param name="code">分类编码</param>
    /// <param name="parentId">父分类ID</param>
    /// <param name="sortOrder">排序号</param>
    /// <param name="description">描述</param>
    public void Update(string name, string code, Guid? parentId = null, int sortOrder = 0, string? description = null)
    {
        Name = name;
        Code = code;
        ParentId = parentId;
        SortOrder = sortOrder;
        Description = description;
        UpdateTime();
    }
}
